package com.yjd.bi.borrow.service.impl;

import com.yjd.bi.borrow.dao.IArrearsFormDao;
import com.yjd.bi.borrow.dao.impl.ArrearsFormDaoImpl;
import com.yjd.bi.borrow.model.ArrearsFormModel;
import com.yjd.bi.borrow.model.BorrowCountModel;
import com.yjd.bi.borrow.service.IArrearsFormService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：还款报表业务实现<br>
 * <b>time</b>：2016-06-30 15:13:29 <br>
 * <b>author</b>：  chenpengfei@yijeidai.com
 */
@Service("arrearsFormService")
public class ArrearsFormServiceImpl extends BaseServiceImpl implements IArrearsFormService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public ArrearsFormModel insert(ArrearsFormModel model) throws Exception {
		return this.arrearsFormDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(ArrearsFormModel model) throws Exception {
		return this.arrearsFormDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.arrearsFormDao.deleteModel(paramMap, true);
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public List<BorrowCountModel> getRecoveryByTime(Long stime) throws Exception{
		if(stime==null){return null;}
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getRecoveryByTime(stime, true);
	}
	
	@SuppressWarnings("unchecked")
	public Long getBorrowerCount(Long start,Long end) throws Exception{
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getBorrowerCount(FrameUtil.newHashMap("repay_starttime_max",end,"repay_starttime_min",start), true);
	}
	
	@SuppressWarnings("unchecked")
	public Double fieldSum(String field,Long start,Long end){
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getFundsSum(FrameUtil.newHashMap("advance",1,"field",field,"advancetime_max",end,"advancetime_min",start), true);
	}
	
	
	@SuppressWarnings("unchecked")
	public Double fieldSelect(String field,Long end,int type){
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).fieldSelect(FrameUtil.newHashMap("field",field,"time",end,"type",type), true);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom,String field) throws Exception {
		EChartsModel ec=new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam=TimeDimensionUtil.getSearchRanges(timeType,timeFrom);
		for(int i=searchParam.size()-1;i>=0;i--){
			ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
			Long end=DateUtil.dateStringToTimestamp(searchParam.get(i).get(1)+"", DateUtil.PATTERN_yyyyMMdd);
			end=end.longValue()+23*60*60+59*60+59;
			Double sum=ArithUtil.round( ((ArrearsFormDaoImpl)this.arrearsFormDao).getRemainSum(FrameUtil.newHashMap("field",field,"endtime",end), false), 2);
			ec.getSeries().get(0).getData().add(NumberUtil.formatNumber(sum,"##0.00"));
		}
		return ec;
	}
	
	public String getFormateX(Long value){
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(value+"", DateUtil.PATTERN_yyyyMMdd),DateUtil.PATTERN_yyyy_MM_dd);
	}
	
	@Resource
	private IArrearsFormDao arrearsFormDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.arrearsFormDao;
	}

	@Override
	public Double getRecoverPrice(long endtime) {
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getRecoverPrice(FrameUtil.newHashMap("endtime",endtime), true);
	}
	
	@Override
	public Double getSumPrice(String field,long starttime,long endtime,int type) {
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getSumPrice(FrameUtil.newHashMap("field",field,"start",starttime,"end",endtime,"type",type), true);
	}

	@Override
	public Double getExpectPrice(Long startTime, Long endTime,Integer depo_type,Integer dz_type) {
		return ((ArrearsFormDaoImpl)this.arrearsFormDao).getExpectPrice(FrameUtil.newHashMap("start",startTime,"end",endTime,"dz_type",dz_type,"depo_type",depo_type), true);
	}

}
